package com.vic.java8.optional;

import java.util.Optional;

/**
 * Java8之妙用Optional解决判断Null问题
 * @author victor
 */
public class OptionalTest {


    public static void main(String[] args) throws Exception {

        // 实例3
        p3();

        // 实例2
//        p2();

        // 实例1
//        p1();

        // ifPresent
//        test3();

        // map
//        test2();

        // orElse, orElseGet
//        test1();
    }

    /**
     * 实例3
     */
    private static void p3() throws Exception {
        // 以前写法
        User user = new User();
        user.setName("a");
        System.out.println(getUser(user));

        // java8写法
        System.out.println(getUser8(user));
    }

    // 以前写法
    public static User getUser(User user) throws Exception {
        if (user != null) {
            String name = user.getName();
            if ("zhangsan".equals(name)) {
                return user;
            } else {
                user = new User();
                user.setName("zhangsan");
                return user;
            }
        } else {
            user = new User();
            user.setName("zhangsan");
            return user;
        }
    }

    // java8写法
    public static User getUser8(User user) {
        return Optional.ofNullable(user)
                .filter(u -> "zhangsan".equals(u.getName()))
                .orElseGet(() -> {
                    User user1 = new User();
                    user1.setName("zhangsan");
                    return user1;
                });
    }

    /**
     * 实例2
     */
    private static void p2() {
        // 以前写法
        User user = new User();
        if(user != null) {
            // todo do sth
        }

        // java8写法
        Optional.ofNullable(user)
                .ifPresent(u -> {
                    // todo do sth
                });
    }

    /**
     * 实例1
     */
    private static void p1() throws Exception {
        User user = new User();
        user.setName("jack");
        System.out.println(getCity(user));
        System.out.println(getCity8(user));
    }

    // java8写法
    public static String getCity8(User user) throws Exception{
        return Optional.ofNullable(user)
                .map(u-> u.getAddress())
                .map(a->a.getCity())
                .orElseThrow(()->new Exception("取指错误"));
    }

    // 以前写法
    public static String getCity(User user)  throws Exception{
        if(user!=null){
            if(user.getAddress()!=null){
                Address address = user.getAddress();
                if(address.getCity()!=null){
                    return address.getCity();
                }
            }
        }
        throw new Exception("取值错误");
    }


    /**
     * 如果用户存在
     */
    private static void test3() {
        User user = new User();
        Optional.ofNullable(user).ifPresent(u -> {
            // do something
        });

    }

    private static void test2() {
        User user = new User();
        user.setName("martin");
        System.out.println(Optional.ofNullable(user).map(u -> u.getName()).get());
    }

    private static void test1() {
        User user = new User();
        user.setName("haha");
        // 当user不为null时，createUser()方法依然执行
//        User user1 = Optional.ofNullable(user).orElse(createUser());
//        System.out.println(user1);

        // 当user不为null时，createUser()方法不执行
        User user2 = Optional.ofNullable(user).orElseGet(() -> createUser());
        System.out.println(user2);
    }

    private static User createUser() {
        System.out.println("createUser（）");
        User u = new User();
        u.setName("菲菲");
        return u;
    }


}
